package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.TOtherFee;
import com.stylefeng.guns.modular.system.service.ITOtherFeeService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 杂项管理控制器
 *
 * @author fengshuonan
 * @Date 2018-10-14 19:56:04
 */
@Controller
@RequestMapping("/tOtherFee")
public class TOtherFeeController extends BaseController {

    private String PREFIX = "/system/tOtherFee/";

    @Autowired
    private ITOtherFeeService tOtherFeeService;

    /**
     * 跳转到杂项管理首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("types", tOtherFeeService.selectType());
        return PREFIX + "tOtherFee.html";
    }

    /**
     * 跳转到添加杂项管理
     */
    @RequestMapping("/tOtherFee_add")
    public String tOtherFeeAdd(Model model) {
        model.addAttribute("types", tOtherFeeService.selectType());
        return PREFIX + "tOtherFee_add.html";
    }

    /**
     * 跳转到修改杂项管理
     */
    @RequestMapping("/tOtherFee_update/{tOtherFeeId}")
    public String tOtherFeeUpdate(@PathVariable Integer tOtherFeeId, Model model) {
        TOtherFee tOtherFee = tOtherFeeService.selectById(tOtherFeeId);
        model.addAttribute("item",tOtherFee);
        LogObjectHolder.me().set(tOtherFee);
        return PREFIX + "tOtherFee_edit.html";
    }

    /**
     * 获取杂项管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String feeType,String applicant,@RequestParam(required = false) String beginTime
            , @RequestParam(required = false) String endTime, Integer offset, Integer limit) {
        Page<Map<String, Object>> page = new Page(offset, limit);
        PageInfoBT pageInfoBT = new PageInfoBT(page);
        Map map=new HashMap();
        map.put("applicant",applicant);
        map.put("feeType",feeType);
        map.put("endTime",endTime);
        map.put("beginTime",beginTime);
        map.put("dot_id",ShiroKit.getUser().getDotId());
        List<Map<String,Object>> maps=tOtherFeeService.selectList(page,map);
        pageInfoBT.setData(maps);
        pageInfoBT.setCount(page.getTotal());
        return pageInfoBT;
    }

    @RequestMapping(value = "/avg")
    @ResponseBody
    public Object avg(String feeType,String applicant,@RequestParam(required = false) String beginTime
            , @RequestParam(required = false) String endTime) {

        Map map=new HashMap();
        map.put("applicant",applicant);
        map.put("feeType",feeType);
        map.put("endTime",endTime);
        map.put("beginTime",beginTime);
        map.put("dot_id",ShiroKit.getUser().getDotId());
        List<Map<String,Object>> maps=tOtherFeeService.selectAvg(map);
        return maps;
    }



    /**
     * 新增杂项管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TOtherFee tOtherFee) {
        tOtherFee.setCreateTime(new Date());
        tOtherFee.setCreateUserId(ShiroKit.getUser().getId());
        tOtherFee.setDetpId(ShiroKit.getUser().getDeptId());
        tOtherFee.setDotId(ShiroKit.getUser().getDotId());
        tOtherFeeService.insert(tOtherFee);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除杂项管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer tOtherFeeId) {
        tOtherFeeService.deleteById(tOtherFeeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改杂项管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TOtherFee tOtherFee) {
        tOtherFeeService.updateById(tOtherFee);
        return super.SUCCESS_TIP;
    }

    /**
     * 杂项管理详情
     */
    @RequestMapping(value = "/detail/{tOtherFeeId}")
    @ResponseBody
    public Object detail(@PathVariable("tOtherFeeId") Integer tOtherFeeId) {
        return tOtherFeeService.selectById(tOtherFeeId);
    }
}
